﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;

namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietDonVi : Form, IChildForm
    {
        DonVi_BUS donvi_bus = new DonVi_BUS();
        public ChiTietDonVi()
        {
            InitializeComponent();
        }

        
        private void ChiTietDonVi_Load(object sender, EventArgs e)
        {
            Nap();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //DonVi_PB.MaDonVi = gridView1.GetRowCellValue(e.FocusedRowHandle, "MADONVI").ToString();
            //DonVi_PB.TenDonVi = gridView1.GetRowCellValue(e.FocusedRowHandle, "TENDONVI").ToString();
            //DonVi_PB.DiaChi = gridView1.GetRowCellValue(e.FocusedRowHandle, "DIACHI").ToString();
            //DonVi_PB.CapToChuc = gridView1.GetRowCellValue(e.FocusedRowHandle, "CAPTOCHUC").ToString();
        }
        public void Them()
        {
            DonVi dv = new DonVi();
            dv.Tag = this;
            dv.ShowDialog();
            Nap();
        }

        public void Sua()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;


            DonVi dv = new DonVi(r["MADONVI"].ToString(), r["TENDONVI"].ToString(),r["DIACHI"].ToString(), r["CAPTOCHUC"].ToString());
            dv.Tag = this;
            dv.ShowDialog();
            Nap();
        }
        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa đơn vị  <{0}>?"
                , r["MADONVI"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                donvi_bus.XoaDonVi(r["MADONVI"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }

        public void Nap()
        {
            gridDonVi.DataSource = donvi_bus.LayDSDonvi();
        }
    }
}
